package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallAddress;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallRegion;
import org.linlinjava.litemall.db.service.LitemallAddressService;
import org.linlinjava.litemall.db.service.LitemallRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.linlinjava.litemall.admin.util.AdminResponseCode.ADMIN_ADDRESS_EXIST;

@RestController
@RequestMapping("/admin/address")
@Validated
public class AdminAddressController {
    private final Log logger = LogFactory.getLog(AdminAddressController.class);

    @Autowired
    private LitemallAddressService addressService;
    @Autowired
    private LitemallRegionService regionService;

    @RequiresPermissions("admin:address:list")
    @RequiresPermissionsDesc(menu = {"用户管理", "收货地址"}, button = "查询")
    @GetMapping("/list")
    public Object list(Integer userId, String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        Subject currentUser = SecurityUtils.getSubject();
        LitemallAdmin loginAdmin = (LitemallAdmin) currentUser.getPrincipal();
        List<LitemallAddress> addressList = addressService.querySelective(userId,loginAdmin.getShopId(), name, page, limit, sort, order);
        return ResponseUtil.okList(addressList);
    }

    @RequiresPermissions("admin:address:regionList")
    @RequiresPermissionsDesc(menu = {"用户管理", "收货地址"}, button = "查询区域")
    @GetMapping("/regionList")
    public Object regionList(Integer parentId) {
        List<LitemallRegion> regionList = regionService.queryByPid(parentId);
        return ResponseUtil.okList(regionList);
    }

    @RequiresPermissions("admin:address:addAddress")
    @RequiresPermissionsDesc(menu = {"用户管理", "收货地址"}, button = "添加地址")
    @PostMapping("/addAddress")
    public Object addAddress(@RequestBody LitemallAddress address) {
        Subject currentUser = SecurityUtils.getSubject();
        LitemallAdmin loginAdmin = (LitemallAdmin) currentUser.getPrincipal();
        if(loginAdmin.getShopId()==null){
            return ResponseUtil.fail(ADMIN_ADDRESS_EXIST, "未获取到商户信息,请勿使用管理员账号添加地址");
        }
        address.setShopId(loginAdmin.getShopId());
        addressService.add(address);
        return ResponseUtil.ok(address);
    }

    @RequiresPermissions("admin:address:updateAddress")
    @RequiresPermissionsDesc(menu = {"用户管理", "收货地址"}, button = "修改地址")
    @PostMapping("/updateAddress")
    public Object updateAddress(@RequestBody LitemallAddress address) {
        addressService.update(address);
        return ResponseUtil.ok(address);
    }

    @RequiresPermissions("admin:address:deleteAddress")
    @RequiresPermissionsDesc(menu = {"用户管理", "收货地址"}, button = "删除地址")
    @PostMapping("/deleteAddress")
    public Object deleteAddress(@RequestBody LitemallAddress address) {
        addressService.delete(address.getId());
        return ResponseUtil.ok(address);
    }




}
